import java.io.*;
import java.nio.charset.Charset;

public class IOFiles {

	public static void main(String[] args) throws IOException {
		InputStream fis;
		BufferedReader br;

		fis = new FileInputStream("source file path .xml");
		br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));

		PrintWriter f0 = new PrintWriter(new FileWriter("destination file path .yaml"));

		convertXML_ToYAML(br, f0);

		f0.close();
		br.close();
		br = null;
		fis = null;

	}

	/**
	 * @param br
	 * @param f0
	 */
	private static void convertXML_ToYAML(BufferedReader br, PrintWriter f0) {
		String line;
		try {
			while ((line = br.readLine()) != null) {
				line = line.replaceAll("\t", " ");
				if (isFirstLine(line)) {
					String kind = getKind(line);
					f0.println("- kind: " + kind);
					f0.println("  properties:");
				} else if (isPropertyLine(line)) {
					String propertyName = getPropertyName(line);
					f0.println("  - name: " + "'"+propertyName+"'");
					String propertyDirection = getPropertyDirection(line);
					f0.println("    direction: " + propertyDirection);
				} else if (line.contains("<!--")) {
					line = line.replaceAll("<!--", "#");
					line = line.replaceAll("-->", "#");
					f0.println(line);
				} else if (isStartOfFile(line)) {
					f0.println("indexes:");
					f0.println();
				} else if (isEndOfFile(line) || isXML_Tage(line) || isLastLine(line)||isAncestor(line)||isSource(line)) {
					//skip
				} else {
					f0.println(line);
				}

			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static boolean isAncestor(String line) {
		return line.contains("ancestor");
	}

	private static String getKind(String line) {
		int lastIndex;
		if(line.contains("ancestor")){
			lastIndex = line.indexOf("ancestor") - 2;
		}else{
			lastIndex = line.lastIndexOf("\"");
		}
		return line.substring(line.indexOf("kind=") + 6, lastIndex);
	}

	private static String getPropertyName(String line) {
		return line.substring(line.indexOf("<property name=") + 16, line.indexOf("direction") - 2);
	}

	private static String getPropertyDirection(String line) {
		return line.substring(line.indexOf("direction=") + 11, line.lastIndexOf("\""));
	}

	private static boolean isFirstLine(String line) {
		return line.contains("<datastore-index kind=");
	}

	private static boolean isLastLine(String line) {
		return line.contains("</datastore-index>");
	}

	private static boolean isPropertyLine(String line) {
		return line.contains("<property name=");
	}

	private static boolean isStartOfFile(String line) {
		return line.contains("<datastore-indexes");
	}

	private static boolean isEndOfFile(String line) {
		return line.contains("</datastore-indexes>");
	}

	private static boolean isXML_Tage(String line) {
		return line.contains("?xml");
	}
	private static boolean isSource(String line) {
		return line.contains("source");
	}
}